<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 30px;
	padding-bottom: 10px;
	}
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
   .mytable .mytable-left{
   text-align: right;
   width : 100px;
   }
  .mytable .mytable-right{
   width : 150px;
   }
</style>
<script type="text/javascript">
//集中获取ModelAndView数据
var path = "${pageContext.request.contextPath}" + "/";
var maintool = null;
var searchPanel = null;
var searchField = null;
var searchButton = null;
var stockGrid = null;
var stockContentGrid = null;
var anaGrid = null;
//设置表格操作提交的网址
var searchUrl = path + "rm/filterQuery.html";
var undoMoveUrl = path + "rm/MatMoveCtl/undoMoveBat.html";

$(function(){
	initVar();
	initLayout();
	initSearchPanel();
	initGrid();
	initComponent();
	search();
});

function initVar(){
	maintool = $("#maintool");
	searchPanel = $("#search-panel");
	searchField = $("#search-field");
	searchButton = $("#search-button");
	stockGrid = $("#stockGrid");
	stockContentGrid = $("#stockContentGrid");
	anaGrid = $("#anaGrid");
}
function initLayout(){
	$('#page').omBorderLayout({
		panels : [ {
			id : "center-panel",
			title : "库存数据",
			region : "center"
		}, {
			id : "west-panel",
			header : false,
			region : "west",
			resizable : true,
			collapsible : true,
			expandToBottom : true,
			width : '30%'
		}, {
			id : "south-panel",
			title : "成分数据",
			region : "south",
			resizable : true,
			height : '32%'
		} ],
		spacing : 3
	});
	searchPanel.omPanel({
		title : "搜索",
		collapsible : true,
		height : 200
	});
}
function initSearchPanel(){
	$(':input',searchField).each(function() {
		$(this).keyup(function(event) {
			if (event.keyCode == 13) {
				search();
			}
		});
	});
	searchButton.omButton({
		icons:{left : path + 'resources/sm/images/search.png'},
		width:70,
		onClick:function(){
			search();
		}
	});
}
function initGrid(){
	stockGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:660,
		colModel : [
				{header : '料仓Id', name : 'storageId', align : 'center', width : '50', sort:'serverSide'},
				{header : '料仓名称', name : 'storageName', align : 'center', width : '130', sort:'serverSide'},
				{header : '重量(KG)', name : 'totalWgt', align : 'center', width : '70', sort:'serverSide'},
		            ],
        onRowClick : function(rowIndex, rowData, event) {
        	var group ={
        			op : "and",
        			rules : [{op : 'equal', field : 'storageId', value : rowData['storageId'], type : 'string'}]
        	};
        	var extraData = {
        			"domain" : "SmRmStorageContent",
        			"rules" : JSON.stringify(group),
        	};
        	stockContentGrid.omGrid({
        		dataSource : searchUrl,
        		extraData : extraData, 
        		method:'POST'
        	}, 'reload');
        }
	});
	stockContentGrid.omGrid({
		limit : 0,
		singleSelect : false,
		height:408,
		colModel : [
				{header : '库存号', name : 'contentId', align : 'center', width : '130', sort:'serverSide'},
				{header : '料仓编码', name : 'storageId', align : 'center', width : '60', sort:'serverSide'},
				{header : '计量单号', name : 'measureId', align : 'center', width : '160', sort:'serverSide'},
				{header : '物料编码', name : 'materialId', align : 'center', width : '80', sort:'serverSide'},
				{header : '物料描述', name : 'materialDesc', align : 'center', width : '120', sort:'serverSide'},
				{header : '重量(KG)', name : 'weight', align : 'center', width : '80', sort:'serverSide'},
				{header : '入库时间', name : 'timeStamp', align : 'center', width : '160', sort:'serverSide'},
		            ],
	});
}
function initComponent(){
	$('#queryStorageType').omCombo({
		dataSource : [ {
			text : '高位料仓',
			value : 'B'
		}, {
			text : '高位料仓-手投',
			value : 'M'
		} , {
			text : '高位料仓-丝线',
			value : 'W'
		} , {
			text : '低位料仓-合金中间仓',
			value : 'ALLOY'
		} , {
			text : '低位料仓-镍库',
			value : 'NI'
		}, {
			text : '低位料仓-手投',
			value : 'MANUAL'
		}, {
			text : '低位料仓-丝线',
			value : 'WIRE'
		} ],
		width : 163,
	});
	
	var buttons = [];
	buttons.push({
		id : "undoMove",
		label : "退回库存",
		onClick : function(){
			try {
				var selections = stockContentGrid.omGrid('getSelections', true);
				if (selections.length > 0) {
					$.omMessageBox.confirm({
						title : '确定退料',
						content : '确定要将库存退回至源料仓？',
						onClose : function(value) {
							if (value) {
								undoMove(selections);
							}
						}
					});
				} else {
					$.omMessageBox.alert({
						content : '请至少选择库存中一行记录，以获取需退回的库存',
					});
				}
			} catch (e) {
				$.omMessageBox.alert({
					content : '请选择料仓和库存',
				});
			}
		}
	});
	buttons.push({separtor : true});
	maintool.omButtonbar({btns:buttons});
}

function search()
{	
	var rules =[], i=0;
	$(':input',searchField).each(function() {
		if($.trim(this.name)!="")
			if( $(this).val() != null &&  $.trim($(this).val()) != "" 
					&& typeof($(this).attr('op')) != "undefined" && typeof($(this).attr('ty')) != "undefined")
				rules[i++] = {op : $(this).attr('op'), field : $(this).attr('name'), value : $(this).val(), type : $(this).attr('ty')};
	});
	var newValue = $('#queryStorageType',searchField).val();
	if(newValue!='' && newValue != 'B' && newValue != 'W' && newValue != 'M'){
		rules[i++] = {op : 'equal', field : 'storageLoc', value : $('#queryStorageType').val(), type : 'string'};
		rules[0] = {op : 'equal', field : 'storageType', value : 'U', type : 'string'};
	}
	var group ={
			op : "and",
			rules : rules,
		};
	 var extraData = {
			"domain" : "VSmRmStorage",
			"rules" : i==0?null:JSON.stringify(group),
		};
	stockGrid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');
}
function undoMove(selections) {
	var ids = "";
	for( var i=0; i<selections.length; i++ )
	{
		var rowData = selections[i];
		if(i!=0){
			ids += ( ","+rowData["contentId"]);
		}else{
			ids += ( rowData["contentId"]);
		}
	}
	if( ids.length > 0){
		var info = {
				"contentIds" : ids
		};
		$.post( undoMoveUrl,info, 
			function(data, textStatus)
			{
		     if( data.success )
				{
		    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
		    	 stockGrid.omGrid( "reload" );
		    	 stockContentGrid.omGrid( "reload" );
				}
		     else
		    	 {
					$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
		    	 }						
			});
	}
}
</script>
	
</head>
<body>
	<div id="page" style="height: 690px;">
		<div id="center-panel" >
			<div id="maintool"></div>			
			<table id="stockContentGrid"></table>					
		</div>
		<div id="west-panel" >
			<div id="search-panel" > 
				<div id="search-field">
				<table id="searchTable" class="mytable">
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓类型：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStorageType" name="storageType" op="equal" ty="string" type="text"/></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓编号：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStorageId" name="storageId" op="startwith" ty="string" type="text" /></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">料仓名称：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStorageName" name="storageName" op="like" ty="string" type="text"/></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">重量大于：</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryWeight" name="weight" op="greaterorequal" ty="string" type="text" /></span></td>
					</tr>
				</table>
				</div>		
				<br />
				<div class="search-button"><span id="search-button">搜索</span></div>							
			</div>
			<table id="stockGrid"></table>
		</div>
		<div id="south-panel">
			<table id="anaGrid"></table>
		</div>			
	</div>	
</body>
</html>